function buscarMarcaP(){
    if($("#buscar").val().length===0){
        return;
    }
    $.ajax(
    {
        type: "GET",
        url: "buscarMarcaP",   
        async: false,
        dataType: "text",
        data: "buscar="+$("#buscar").val(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#body-content").html(transport);
            $('input[id="btnEliminar"]').click(eliminarMarcaP);
            $('input[id="btnModificar"]').click(modificarMarcaP);
            $('#btnAgregar').click(agregarMarcaP);
            $('#btnBuscar').click(buscarMarcaP);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function modificarMarcaAP(){
    if(!validaAltaMarcaP()){
        alert("Campos invalidos");
        return false;
    }
        
    $.ajax(
    {
        type: "POST",
        url: "modificarMarcaP",
        async: false,
        dataType: "text",
        data: $('#modficarMarcaF').serialize(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport);
            $("#body-content").html("");
            $('#menuMarca').trigger('click');
                               
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
    $('#menuMarca').trigger('click');
    $('#menuMarca').trigger('click');
    return false;                        
}

function modificarMarcaP(){    
    $("#tablaMarca").hide();
    
    $.ajax(
    {
        type: "GET",
        url: "gestionModificarMarcaPreP",
        async: false,
        dataType: "text",
        data: "idMarca="+$(this).closest('tr').attr("id"),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verMarca").html(transport);
            $(".error-inner").hide();
            $("#verMarca").show();
            $("#tablaMarca").hide();
            $('#btnModificarMarca').click(modificarMarcaAP);
            $("#btnRegresar").click(mostrarMarcaP);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function eliminarMarcaP(){ 
     id = $(this).closest('tr').attr("id");
    if(!confirm("Seguro que desea eliminar la marca "+ id))
        return;   
    $.ajax(
    {
        type: "GET",
        url: "eliminarMarcaP",
        async: false,
        dataType: "text",
        data: "idMarca="+ id,
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport); 
            $("#"+id).remove();
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function mostrarMarcaP(){
    $("#verMarca").hide();   
    $("#tablaMarca").show();
}

function agregarMarcaP(){  
    $("#tablaMarca").hide();
    
    $.ajax(
    {
        type: "GET",
        url: "GestionMarcas/gestionAgregarMarca.jsp",
        async: false,
        dataType: "text",
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            $("#verMarca").html(transport);
            $(".error-inner").hide();
            $("#verMarca").show();
            $("#tablaMarca").hide();
            $('#btnAltaMarca').click(altaMarcaP);
            $("#btnRegresar").click(mostrarMarcaP);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
}

function altaMarcaP(){
    if(!validaAltaMarcaP())
        return false;
       
    $.ajax(
    {
        type: "POST",
        url: "altaMarcaP",
        async: false,
        dataType: "text",
        data: $('#altaMarca').serialize(),
        beforeSend: function(){
                    
        },
        success: function(transport)
        {
            alert(transport);
            $("#body-content").html("");
            $('#menuMarca').trigger('click');
                               
        },
        error: function (xhr, ajaxOptions, thrownError) {
            $('#errores').html(xhr.responseText);
            alert(thrownError);
        }            
    });
    $('#menuMarca').trigger('click');
    $('#menuMarca').trigger('click');
    return false;
}

function validaAltaMarcaP(){
    $('.error-inner').hide();
    esValido = true;
    if($('#Nmarca').val().length<2){
        $("#Nmarcae").show();
        esValido = false;
    }    
    return esValido;
}
